package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

import org.springframework.web.multipart.MultipartFile;
import util.ImageCut;
import util.UploadImg;

/**
 * Created by luolei on 15/11/3.
 */
@Controller
public class IndexController {

//    @Resource
//    private UserServiceImpl service;

    @RequestMapping(value = "register")
    public String register() {
        return "logreg";
    }

    @RequestMapping(value = "/")
    public String home() {
        return "home";
    }

    @RequestMapping(value = "noticeInfo")
    public String notice() {
        return "noticeInfo";
    }

//    @RequestMapping(value = "login")
//    public String login(String username, String password) {
//        if (service.login(username, password))
//            return "hello";
//        else
//            return "error";
//    }

    @RequestMapping(value = "login")
    public void login(@RequestParam("email") String email, @RequestParam("password") String password,
                      PrintWriter out,HttpServletRequest request) {
        request.getSession().setAttribute("user", "nickName");
        out.print("nickName");
    }
    @RequestMapping(value = "managerCenter")
    public String managerCenter() {
        return "managerCenter";
    }

    @RequestMapping(value = "personalInfo")
    public String personalInfo() {
        return "personalInfo";
    }

    @RequestMapping(value = "authorCenter")
    public String authorCenter() {
        return "authorCenter";
    }

    @RequestMapping(value = "searchPro")
    public String searchPro() {
        return "searchPro";
    }

    @RequestMapping(value = "editorCenter")
    public String editorCenter() {
        return "editorCenter";
    }

    @RequestMapping(value = "accArticleList")
    public String accArticleList() {
        return "accArticleList";
    }

    @RequestMapping(value = "professorCenter")
    public String professorCenter() {
        return "professorCenter";
    }

    @RequestMapping(value = "articleDetail")
    public String articleDetail1() {
        return "articleDetail";
    }

    @RequestMapping(value = "office")
    public String office() {
        return "office";
    }

    @RequestMapping(value = "help")
    public String help() {
        return "help";
    }

    @RequestMapping(value = "pdf")
    public String pdf() {
        return "pdf";
    }

    @RequestMapping(value = "noticeCon")
    public String noticeCon() {
        return "noticeCon";
    }

    @RequestMapping(value = "editor")
    public String editor() {
        return "editor";
    }

    @RequestMapping(value = "upImg")
    public void upImg(@RequestParam(value="upload") MultipartFile fileField, @RequestParam(value = "CKEditorFuncNum") String callback,
                      HttpServletRequest request, PrintWriter out) throws Exception {
        String filePath = request.getServletContext().getRealPath("/img");
        String fileName = UploadImg.uploadImg(filePath, fileField, 3456);
        out.println("<script type=\"text/javascript\">");
        out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
                + ",'" + request.getContextPath() + "/img/" + fileName + "','')");
        out.println("</script>");
    }

    @RequestMapping(value = "contributeForm", method = RequestMethod.POST)
    public String contributeForm(@RequestParam(value = "title") String title, @RequestParam(value = "keyword") String keyword,
                                 @RequestParam(value = "type") String type, @RequestParam(value = "summary") String summary,
                                 @RequestParam(value="articleFile") MultipartFile articleFile, HttpServletRequest request) throws Exception {
        System.out.println(title);
        System.out.println(keyword);
        System.out.println(type);
        System.out.println(summary);
        String filePath = request.getServletContext().getRealPath("/file");
        System.out.println(UploadImg.uploadImg(filePath, articleFile, 3456));
        return "articleDetail";
    }



    @RequestMapping(value = "/AcceptImg", method = RequestMethod.POST)
    public void uploadImg(  @RequestParam(value="fileField") MultipartFile fileField, HttpServletRequest request,
                            PrintWriter out) throws Exception{
        String filePath = request.getServletContext().getRealPath("/img");
        String imgName = UploadImg.uploadImg(filePath, fileField, 3456);
        out.print(imgName);
    }
    @RequestMapping(value = "/changeImg", method = RequestMethod.POST)
    public void uploadImg(  @RequestParam(value="x") int x, @RequestParam(value="y") int y,
                            @RequestParam(value="width") int width, @RequestParam(value="height") int height,
                            HttpServletRequest request, @RequestParam(value = "imgName") String imgName,
                            PrintWriter out) throws Exception{
        String imgPath = request.getServletContext().getRealPath("/img");
        String srcImageFile = imgPath + "/" + imgName;
        String dirImageFile = imgPath + "/" + imgName;
        ImageCut.abscut(srcImageFile, dirImageFile, x, y, width, height);
        out.print("yes");
    }

}
